<template>
	<view class="page">
		<view class="summary-container">
			<image :src="photo" mode="widthFix" class="photo"></image>
			<view class="summary">
				<view class="row">
					<text class="name">{{ name }}</text>
					<image :src="realAuth ? '../../static/person/icon-1.png' : '../../static/person/icon-2.png'"
						mode="widthFix" class="auth-icon"></image>
					<text :class="realAuth ? 'auth' : 'unauth'">{{ realAuth ? '已认证' : '未认证' }}</text>
				</view>
				<view class="row">
					<view class="attribute">
						<view class="item">代驾：{{ driverExp }}年</view>
						<view class="item">等级：Lv{{ level }}</view>
						<view class="item">接单：{{ totalOrder }}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="notice-container">
			<view class="notice-title">
				<image src="../../static/person/icon-3.png" mode="widthFix"></image>
				<text>{{ levelName }}</text>
			</view>
			<view class="desc">升级Lv{{ level + 1 }}等级还需接单{{ 50 - (totalOrder % 50) }}个，并且为顾客提供安全和高质量代驾服务</view>
		</view>
		<view class="nav-container">
			<view class="row">
				<view class="item" @tap="toPage('../../wallet/recharge/recharge')">
					<image src="../../static/person/icon-4.png" mode="widthFix"></image>
					<view class="info">
						<text class="title">钱包</text>
						<text class="desc">总额{{ balance }}元</text>
					</view>
				</view>
				<view class="border-column"></view>
				<view class="item">
					<image src="../../static/person/icon-5.png" mode="widthFix"></image>
					<view class="info">
						<text class="title">订单</text>
						<text class="desc">本周有{{ weekOrder }}单</text>
					</view>
				</view>
			</view>
			<view class="border-row"></view>
			<view class="row">
				<view class="item">
					<image src="../../static/person/icon-6.png" mode="widthFix"></image>
					<view class="info">
						<text class="title">客户评价</text>
						<text class="desc">近期{{ weekComment }}条好评</text>
					</view>
				</view>
				<view class="border-column"></view>
				<view class="item">
					<image src="../../static/person/icon-7.png" mode="widthFix"></image>
					<view class="info">
						<text class="title">我的申诉</text>
						<text class="desc">正在申诉{{ appeal }}件</text>
					</view>
				</view>
			</view>
		</view>

		<u-cell-group :border="false">
			<u-cell icon="account-fill" :icon-style="icon" title="账号与安全" :border-top="false" />
			<u-cell icon="eye-fill" :icon-style="icon" title="接单热点地区" />
			<u-cell icon="server-fill" :icon-style="icon" title="在线客服" @click="serviceHandle" />
			<u-cell icon="trash-fill" :icon-style="icon" title="清理缓存" @click="clearCacheHandle" />
			<u-cell icon="file-text-fill" :icon-style="icon" title="用户指南" :border-bottom="false" />
		</u-cell-group>
		<view class="exit"><button class="btn" @tap="logoutHandle()">退出系统</button></view>
		<u-toast ref="uTips"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				icon: {
					color: '#8A8B8F',
					'margin-top': '-1rpx'
				},
				name: '',
				photo: '',
				realAuthStatus: false,
				driverExp: 0, //代驾年龄
				level: 0,
				levelName: '',
				balance: 0,
				totalOrder: 0,
				weekOrder: 0,
				weekComment: 0,
				appeal: 0
			};
		},
		methods: {
			logoutHandle() {
				this.showConfirmModel();
			},

			showConfirmModel: function() {
				let that = this;
				uni.vibrateShort({}); //手机震动函数
				uni.showModal({
					title: "提示信息",
					content: "确认要退出系统吗？",
					success: function(resp) {
						if (resp.confirm) {
							//调用后台退出
							that.performLogOut();
						}
					}
				})
			},
			//后台登出
			performLogOut() {
				let that = this;
				this.request(this.urls.logout, 'POST', null, function() {
					//清空缓存里面的token realAuth
					uni.removeStorageSync('realAuth');
					uni.removeStorageSync('token');
					//回到首页
					that.redirectToLogin();
				});

			},
			//返回首页
			redirectToLogin: function() {
				uni.showToast({
					title: "已经退出系统",
					success() {
						uni.redirectTo({
							url: '../index/index'
						})
					}
				})
			},
			//拨打的话
			serviceHandle() {
				uni.vibrateShort({});
				uni.makePhoneCall({
					phoneNumber: '400-1234-6789'
				})
			},

			//清理缓存
			clearCacheHandle() {
				let that = this;
				uni.vibrateShort({});
				uni.showModal({
					title: "提示信息",
					content: "清理本地缓存",
					success: function(resp) {
						if (resp.confirm) {
							//清理缓存
							that.clearLocalStorageAndFiles()
						}
					}
				})

			},

			clearLocalStorageAndFiles() {
				uni.vibrateShort({});
				uni.showLoading({
					title: "缓存清理中"
				});
				//清理缓存（除token和realAuth）
				this.clearStorage();
				//清理缓存文件
				this.clearCacheFiles();

				uni.hideLoading();

			},

			clearStorage() {
				let cache = uni.getStorageInfoSync();
				for (let key of cache.keys) {
					if (key != 'token' && key != 'realAuth') {
						uni.removeStorageSync(key);
					}
				}

			},
			clearCacheFiles() {
				uni.getSavedFileList({
					success: function(res) {
						console.log(res.fileList);
						for (let f of res.fileList) {
							console.log("缓存文件", f.filePath);
							let path = f.filePath;
							uni.removeSavedFile({
								filePath: path,
								success() {
									console.log("缓存文件删除成功!");
								}
							})

						}

					}
				});

			},

		},
		onShow() {
			let that = this;
			this.request(this.urls.searchDriverBaseInfo, 'POST', null, function(resp) {
				console.log("司机基本信息：", resp);
				let result = resp.data;
				that.name = result.name;
				that.photo = result.photo;
				that.levelName = result.levelName;
				that.level = result.level;
				that.driverExp = result.driverExp;
				that.realAuthStatus = uni.getStorageInfoSync('realAuth') == 2;
				that.balance = result.balance;
				that.totalOrder = result.summary.totalOrder;
				that.weekOrder = result.summary.weekOrder;
				that.weekComment = result.summary.weekComment;
				that.appeal = result.summary.appeal;
			});
		}
	}
</script>

<style lang="less">
	@import url("person.less");
</style>